{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rklMFAsZnk_9"
      },
      "source": [
        "# Exercise Set 1\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xxzIRHkEw3Um"
      },
      "source": [
        "## Multiple choice\n",
        "\n",
        "Correct answers are `marked like this`.\n",
        "\n",
        "**Q1.** All projects that can utilize mathematical optimization identify this well in advance.\n",
        "> TRUE\n",
        "\n",
        "> `FALSE`\n",
        "\n",
        "**Q2.** The core value of machine learning lies in the _________ used to fit models. In mathematical optimization, the _________ carries most of the value.\n",
        ">a. platform, GPU\n",
        "\n",
        ">b. `data, algorithms`\n",
        "\n",
        ">c. algorithms, industry\n",
        "\n",
        ">d. parallelization, data\n",
        "\n",
        "**Q3.** Heuristics often have a way to know how far a current solution may be from the optimal solution.\n",
        "> TRUE\n",
        "\n",
        "> `FALSE`\n",
        "\n",
        "**Q4.** While it is good to note the size of a mathematical optimization problem in terms of the number of variables and constraints, it is far from a reliable way to measure how long it will take to solve.\n",
        "> `TRUE`\n",
        "\n",
        "> FALSE"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4g1g9c6LpDCd"
      },
      "source": [
        "## Free Responses\n",
        "````\n",
        "I suggest getting some paper and pencil here for some of these\n",
        "````\n",
        "**Q5.** Apply `DeMorgan's laws` to the following statements. Final answers should only use the $=$, $\\le$, $\\ge$, $<$, and $>$ comparison symbols, where applicable.\n",
        ">a. Not (Python or R).\n",
        ">>not Python and not R\n",
        "\n",
        ">b. It's not the case that both $x$ is equal to 0 and $y$ is positive. Simplify this statement so all \"*not*\" are removed.\n",
        "\n",
        "$$\n",
        "\\begin{align*}\n",
        "&not \\space (x = 0 \\space or \\space y > 0)\\\\\n",
        "&not \\space x = 0 \\space and \\space not \\space y > 0\\\\\n",
        "&x > 0  \\space or \\space x < 0\\space and \\space y \\le 0\n",
        "\\end{align*}\n",
        "$$\n",
        "\n",
        "**Q6.** Let $a$ be the amount of snow that falls overnight and $b$ is a binary variable indicating if school is open ($b=1$) or closed ($b=0$). Use this, and the `IF-THEN` to an `OR` statement equivalence to rewrite this statement: \"If it snows more than 8 inches overnight, then school will be closed tomorrow.\"\n",
        ">> If $a > 8$ then $b = 0$\n",
        "\n",
        ">> $a \\le 8$ or $b=0$\n",
        "\n",
        "**Q7.** Write the `contrapositive` of \"If Duke is late for a meeting, that's because he was out surfing.\"\n",
        "\n",
        ">> If Duke wasn't out surfing, then he won't be late for a meeting.\n",
        "\n",
        "**Q8.** In the **Using Big M Constraints** section of last session's notebook, we used the following inequality $x + M_2 \\times z \\ge C$.\n",
        ">a. Simplify this inequality replacing $𝑀_2$ with $C$.\n",
        "$$\n",
        "\\begin{align*}\n",
        "x + M_2 \\times z \\ge C\\\\\n",
        "x + C \\times z \\ge C\\\\\n",
        "x  \\ge C - C \\times z\\\\\n",
        "x \\ge C×(1-z)\n",
        "\\end{align*}\n",
        "$$\n",
        "\n",
        ">b. The other clause of this `OR` statement was $x \\le M_1\\times(1-z)$. Write out what these two inequalities reduce to when $z = 1$ and $z = 0$.\n",
        "\n",
        "> Let $z = 1$\n",
        "$$\n",
        "\\begin{align*}\n",
        "x \\ge C×(1-z) \\quad\\quad & \\space\\quad\\quad x \\le M_1\\times(1-z)\\\\\n",
        "x \\ge C×(1-1) \\quad\\quad & \\space\\quad\\quad x \\le M_1\\times(1-1)\\\\\n",
        "x \\ge C×0 \\quad\\quad & \\space\\quad\\quad x \\le M_1\\times0\\\\\n",
        "x \\ge 0 \\quad\\quad & \\space\\quad\\quad x \\le 0\\\\\n",
        "x & = 0\n",
        "\\end{align*}\n",
        "$$\n",
        "> Let $z = 0$\n",
        "$$\n",
        "\\begin{align*}\n",
        "x \\ge C×(1-z) \\quad\\quad & \\space\\quad\\quad x \\le M_1\\times(1-z)\\\\\n",
        "x \\ge C×(1-0) \\quad\\quad & \\space\\quad\\quad x \\le M_1\\times(1-0)\\\\\n",
        "x \\ge C×1 \\quad\\quad & \\space\\quad\\quad x \\le M_1\\times1\\\\\n",
        "x \\ge C \\quad\\quad & \\space\\quad\\quad x \\le M_1\\\\\n",
        "C \\le x & \\le M_1\n",
        "\\end{align*}\n",
        "$$"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0HiQb2A8RUHy"
      },
      "source": [
        "## Formulation and Coding\n",
        "**Q9.** In the **Constraining production facilities** section of the Modeling Example 1 notebook, when adding the $y$ decision variables for opening production facilities, assume there are fixed costs given by the list `[180, 150, 120, 155, 175]` for activating each respective production facility. Write `gurobipy` code that will add these costs to the original objective function.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "TDjRv8vJAYXT"
      },
      "outputs": [],
      "source": [
        "# the quickest way is to incorporate this when defining the variable.\n",
        "y = m.addVars(production, obj = [180, 150, 120, 155, 175], vtype=GRB.BINARY)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-bo0xP8tAd0Q"
      },
      "source": [
        "**Q10.** Write `gurobipy` code using indicator constraints to model the following. Both parts reference the decision variables in the Modeling Example 1 notebook.\n",
        ">a. The link between $x_{p,d}$ and $y_p$.\n",
        "\n",
        ">b. If Cleveland and Baltimore are open, then Charleston must be closed. Feel free to add *auxiliary variables* if you want. "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "TM8a0FOfyInf"
      },
      "outputs": [],
      "source": [
        "# part a.\n",
        "m.addConstrs(((y[p] == 0) >> (x[p,d] == 0)) for p in production for d in distribution)\n",
        "\n",
        "# part b.\n",
        "z = m.addVar(vtype = GRB.BINARY, name = 'z')\n",
        "\n",
        "# take the contrapositive\n",
        "# if charleston is open then NOT (cleveland and baltimore open)\n",
        "# cleveland and baltimore both open means y_cle + y_bal = 2. \n",
        "# not that means y_cle + y_bal <= 1\n",
        "m.addConstr((y['Charleston'] == 1) >> (y['Cleveland'] + y['Baltimore'] <= 1))"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": [],
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
